<?php

/**
 * The DbGet class has methods query the database.
 * 
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
class DbGet extends Db {

    /**
     * Get the access logs
     * 
     * @param int $id_account The identity of the account
     * @param string $type_account The type of the account
     * @param boolean $last [optional]<br>
     *                      Sets whether to get only the log of last access
     * @return boolean|array|stdClass The access logs of the account<br>
     *                                or <b>FALSE</b> if no result
     */
    public static function accesslogs($id_account, $type_account, $last = false) {
        $query = 'SELECT * FROM `accesslogs`
            WHERE `id_account`=? AND `type_account`=?';
        if ($last)
            $query .= ' ORDER BY `date` DESC LIMIT 1, 1';
        $rs = parent::get($query, $id_account, $type_account);
        if (isset($rs->data)) {
            if ($rs->fetch) {
                $data[] = $rs->data;
                do {
                    $rs = parent::get($query, $id_account, $type_account);
                    if (isset($rs->data))
                        $data[] = $rs->data;
                } while ($rs->fetch);
            }
            else
                $data = $rs->data;
            return $data;
        }
        else
            return false;
    }

    /**
     * Get the account
     * 
     * @param int|string $arg1 The identity or email
     * @param string $arg2 The type or password
     * @return boolean|stdClass The data of the account or <b>FALSE</b> if no result
     */
    public static function account($arg1, $arg2) {
        $query = 'SELECT * FROM `accounts`';
        if (Util::is_md5($arg1))
            $query .= ' WHERE md5(`id`)=? AND md5(`type`)=?';
        elseif (is_numeric($arg1))
            $query .= ' WHERE `id`=? AND `type`=?';
        else
            $query .= ' WHERE `email`=? AND `password` LIKE ?';
        $rs = parent::get($query, $arg1, $arg2);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the banners
     * 
     * @param string $type The type of banner<br>
     *                  <b>1</b> if top banner,<br>
     *                  <b>2</b> if tv banner,<br>
     *                  <b>3</b> if middle banner,<br>
     *                  <b>4</b> if lateral banner,<br>
     *                  <b>5</b> if search banner or<br>
     *                  <b>6</b> if lateral two banner
     * @param int $limit [optional]<br>The number of limit
     * @param boolean $allview [optional]<br>Sets to view the viewed and non-viewed
     * @return array The data of the banners
     */
    public static function banners($type, $limit = null, $allview = false) {
        $query = 'SELECT b.`filepicture`, `id_enterprise`, `href`, `target`,
            e.`id`, `fancyname`
            FROM `banners` AS b
            LEFT JOIN `enterprises` AS e ON (e.`id`=b.`id_enterprise`)
            WHERE `type`=? and `expiration`>"' . date("Y-m-d") . '"';
        if (!$allview)
            $query .= ' AND `viewed` IS NULL';
        $query .= ' ORDER BY RAND()';
        if (is_numeric($limit))
            $query .= ' LIMIT ' . $limit;
        do {
            $rs = parent::get($query, $type);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the brands of vehicles
     * 
     * @param int $id_type The identity of the type of vehicle
     * @param int|string $arg [optional]<br>The identity or name
     * @return array The data of the brands of vehicles
     */
    public static function brands($id_type, $arg = null) {
        $query = 'SELECT DISTINCT(b.`id`), b.`name`, `detached`
            FROM `brands` AS b
            INNER JOIN `models` AS m ON (m.`id_brand`=b.`id`)
            WHERE `id_type`=?';
        if ($arg != null) {
            $query .= ' AND ';
            if (is_numeric($arg))
                $query .= 'b.`id`=?';
            else
                $query .= 'b.`name`=?';
        }
        $query .= ' ORDER BY b.`detached` DESC, b.`name`';
        do {
            if ($arg == null)
                $rs = parent::get($query, $id_type);
            else
                $rs = parent::get($query, $id_type, $arg);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        if ($arg != null && is_array($data) && count($data) == 1)
            $data = $data[0];
        return $data;
    }

    /**
     * Get the cities
     * 
     * @param int|string $state The identity or symbol of the state
     * @return array The data of the cities
     */
    public static function cities($state) {
        if (is_numeric($state))
            $query = 'SELECT `id`, `name` FROM `cities` WHERE `id_state`=? ORDER BY `name`';
        else
            $query = 'SELECT c.`id`, c.`name` FROM `cities` AS c
                INNER JOIN `states` AS s ON (s.`id`=c.`id_state`)
                WHERE s.`symbol`=? ORDER BY c.`name`';
        do {
            $rs = parent::get($query, $state);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the city
     * 
     * @param int|string $arg The identity or name
     * @return stdClass The data of the city
     */
    public static function city($arg) {
        $query = 'SELECT `id`,`name` FROM `cities` WHERE ';
        if (is_numeric($arg))
            $query .= '`id`=?';
        else
            $query .= '`name`=?';
        $query .= ' ORDER BY `name`';
        $rs = parent::get($query, $arg);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the colors
     * 
     * @return array The data of the colors
     */
    public static function colors() {
        $query = 'SELECT `id`,`name` FROM `colors` ORDER BY `name`';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the customer
     * 
     * @param int $id The identity
     * @param int $cpf [optional]<br>The CPF
     * @return boolean|stdClass The data of the customer or <b>FALSE</b> if no result
     */
    public static function customer($id, $cpf = null) {
        if ($id == null && $cpf == null)
            return false;

        $query = 'SELECT * FROM `customers` AS c
            INNER JOIN `accounts` AS a ON (a.`id`=c.`id`)
            INNER JOIN `cities` AS ct ON (ct.`id`=c.`id_city`)
            INNER JOIN `states` AS st ON (ct.`id_state` = st.`id`)
            WHERE a.`type`=? AND ';
        if ($id != null) {
            $query .= 'c.`id`=?';
            $arg = $id;
        } else {
            $query .= 'c.`cpf`=?';
            $arg = $cpf;
        }

        $rs = parent::get($query, 'F', $arg);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the customer related with vehicle
     * 
     * @param int $id_vehicle The identity of the vehicle
     * @return boolean|stdClass The data of the customer or <b>FALSE</b> if no result
     */
    public static function customer_vehicle($id_vehicle) {
        $query = 'SELECT * FROM `customers` AS c
            INNER JOIN `customers_vehicles` AS cv ON (cv.`id_customer`=c.`id`)
            WHERE cv.`id_vehicle`=?';
        $rs = parent::get($query, $id_vehicle);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the ad of vehicle of the customer
     * 
     * @param int $id The identity
     * @return boolean|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function customer_vehicle_ad($id) {
        $query = 'SELECT * FROM `customers_vehicles` AS cv
            INNER JOIN `vehicles` AS v ON (v.`id`=cv.`id_vehicle`)
            INNER JOIN `models` AS m ON (m.`id`=v.`id_model`)
            WHERE cv.`id`=?';
        $rs = parent::get($query, $id);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the vehicles ads of the customer
     * 
     * @param int $id_customer The identity of the customer
     * @return boolean|stdClass The data of the vehicles ads or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function customer_vehicles($id_customer) {
        $query = 'SELECT cv.`id`, s.`id`, s.`name`,`dateinclusion`, m.`name`,
            `firstreg`, `modelyear`, v.`price`, `discount`, `paydate`,
            `renavam`, `daysamount`,`plate`,c.`name` as color,
            DATE_ADD(ct.`paydate`, INTERVAL p.`daysamount` DAY) AS expired, ve.`name` as version
            FROM `customers_vehicles` AS cv
            INNER JOIN `status` AS s ON (s.`id`=cv.`id_status`)
            INNER JOIN `plans` AS p ON (p.`id`=cv.`id_plan`)
            INNER JOIN `vehicles` AS v ON (v.`id`=cv.`id_vehicle`)
            INNER JOIN `models` AS m ON (m.`id`=v.`id_model`)
            INNER JOIN `colors` AS c ON (c.`id`=v.`id_color`)
            LEFT JOIN `versions` AS ve ON (ve.`id`=v.`id_version`)
            LEFT JOIN `customers_transactions` AS ct ON (`id_customer_vehicle`=cv.`id`)
            WHERE `id_customer`=?
            GROUP BY cv.`id`';
        do {
            $rs = parent::get($query, $id_customer);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    public static function customers_transactions_paydate($id = NULL) {
        if (isset($id)) {
            $idquery = 'cv.`id_customer` = ' . $id;
        } else {
            $idquery = 'DATE_ADD(ct.`paydate`, INTERVAL p.`daysamount` DAY) < CURDATE() and cv.`id_status`=3';
        }
        $query = '
                SELECT cv.`id` FROM  `customers_vehicles` AS cv 
                INNER JOIN `customers_transactions` AS ct ON ct.`id_customer_vehicle` = cv.`id`
                INNER JOIN `plans` p on p.`id`=cv.`id_plan`
                WHERE ' . $idquery;
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    public static function products_transactions_paydate($id = NULL) {
        $query = '(SELECT "F" AS type,
                    ptoo.`id_client`,
                    cl.`name` AS clname,
                    pl.price
                    FROM `products_owners` AS ptoo
                    INNER JOIN `customers` AS cl ON (cl.`id`=ptoo.`id_client` AND ptoo.`type`="F")
                    INNER JOIN `products_transactions` AS ptot ON (ptot.`id_client`=ptoo.`id_client` AND ptot.`type`=ptoo.`type`)
                    INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
                    INNER JOIN `plans` AS pl ON (pl.`id`=cl.`id_product_plan`)
                    WHERE DATE_ADD(ptot.`paydate`, INTERVAL pl.`daysamount` DAY)<CURDATE() and pcs.`id_status`=3 and cl.`alertpay`
                    GROUP BY cl.`id`
                    )
                    UNION
                    (SELECT
                        "J" AS type,
                        ptoo.`id_client`,
                        cl.`fancyname` AS clname,
                        pl.price
                    FROM `products_owners` AS ptoo
                    INNER JOIN `enterprises` AS cl ON (cl.`id`=ptoo.`id_client` AND ptoo.`type`="J")
                    INNER JOIN `products_transactions` AS ptot ON (ptot.`id_client`=ptoo.`id_client` AND ptot.`type`=ptoo.`type`)
                    INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
                    INNER JOIN `plans` AS pl ON (pl.`id`=cl.`id_product_plan`)
                    WHERE DATE_ADD(ptot.`paydate`, INTERVAL pl.`daysamount` DAY)<CURDATE() and pcs.`id_status`=3 and cl.`alertpay`
                    GROUP BY cl.`id`
                    )ORDER BY clname';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the details of vehicled
     * 
     * @param int $id_type The identity of the type of vehicle
     * @return boolean|array The data of the details of vehicles or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function details($id_type) {
        $query = 'SELECT * FROM `details` WHERE `id_type`=?';
        do {
            $rs = parent::get($query, $id_type);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the differentials of vehicled
     * 
     * @return boolean|array The data of the differentials of vehicles or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function differentials() {
        $query = 'SELECT * FROM `differentials`';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the enterprise
     * 
     * @param int $id The identity
     * @param int $cnpj [optional]<br>The CNPJ
     * @return boolean|stdClass The data of the enterprise or <b>FALSE</b> if no result
     */
    public static function enterprise($id, $cnpj = null) {
        if ($id == null && $cnpj == null)
            return false;

        $query = 'SELECT * FROM `enterprises` AS e
            INNER JOIN `accounts` AS a ON (a.`id`=e.`id`)
            INNER JOIN `status` AS s ON (s.`id`=e.`id_status`)
            INNER JOIN `cities` AS ct ON (ct.`id`=e.`id_city`)
            INNER JOIN `states` AS st ON (st.`id`=ct.`id_state`)
            LEFT JOIN `plans` AS p ON (p.`id`=e.`id_plan`)
            WHERE a.`type`=? AND ';
        if ($id != null) {
            $query .= 'e.`id`=?';
            $arg = $id;
        } else {
            $query .= 'e.`cnpj`=?';
            $arg = $cnpj;
        }

        $rs = parent::get($query, 'J', $arg);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the transactions of the enterprise
     * 
     * @param int $id_enterprise The identity of the enterprise
     * @param int $id [optional]<br>The identity
     * @return boolean|array|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function enterprise_transactions($id_enterprise, $id = null) {
        $query = 'SELECT * FROM `enterprises_transactions` WHERE `id_enterprise`=?';
        if ($id != null) {
            $query .= ' AND `id`=?';
            $query .= ' ORDER BY `paydate` desc';
            $rs = parent::get($query, $id_enterprise, $id);
            return $rs->data;
        } else {
            do {
                $rs = parent::get($query, $id_enterprise);
                if (isset($rs->data))
                    $data[] = $rs->data;
            } while ($rs->fetch);
            return $data;
        }
    }

    public static function enterprises_transactions_paydate($id = NULL) {
        if (isset($id)) {
            $idquery = 'e.`id` = ' . $id . ' and';
        } else {
            $idquery = 'DATE_ADD(et.`paydate`, INTERVAL pl.`daysamount` DAY) < CURDATE() and  e.`id_status`=3 and';
        }
        $query = 'SELECT e.`id`,DATE_ADD(et.`paydate`, INTERVAL pl.`daysamount` DAY) as `expiration`,et.`paydate` from `enterprises` AS e
                INNER JOIN `enterprises_transactions` AS et ON et.`id_enterprise`=e.`id`
                INNER JOIN  `plans` AS pl ON pl.`id`=e.`id_plan`
                WHERE ' . $idquery . ' et.`id` = 
                (select `id` from `enterprises_transactions` WHERE `id_enterprise`=e.`id` ORDER BY `paydate` DESC LIMIT 1
                )GROUP BY et.`id_enterprise`
                ORDER BY et.`paydate`
                ';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the enterprise related with vehicle
     * 
     * @param int $id_vehicle The identity of the vehicle
     * @return boolean|stdClass The data of the enterprise or <b>FALSE</b> if no result
     */
    public static function enterprise_vehicle($id_vehicle) {
        $query = 'SELECT * FROM `enterprises` AS e
            INNER JOIN `enterprises_vehicles` AS ev ON (ev.`id_enterprise`=e.`id`)
            INNER JOIN `cities` AS ct ON (ct.`id`=e.`id_city`)
            INNER JOIN `states` AS st ON (st.`id`=ct.`id_state`)
            WHERE ev.`id_vehicle`=?';
        $rs = parent::get($query, $id_vehicle);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the ad of vehicle of the enterprise
     * 
     * @param int $id The identity
     * @return boolean|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function enterprise_vehicle_ad($id) {
        $query = 'SELECT * FROM `enterprises_vehicles` AS ev
            INNER JOIN `vehicles` AS v ON (v.`id`=ev.`id_vehicle`)
            INNER JOIN `models` AS m ON (m.`id`=v.`id_model`)
            WHERE ev.`id`=?';
        $rs = parent::get($query, $id);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the vehicles ads of the enterprise
     * 
     * @param int $id_enterprise The identity of the enterprise
     * @return boolean|stdClass The data of the vehicles ads or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function enterprise_vehicles($id_enterprise) {
        $query = 'SELECT ev.`id`, s.`id`, s.`name`,`dateinclusion`, m.`name`,
            `firstreg`, `modelyear`, v.`price`, `discount`, `paydate`, `renavam`,`plate`,c.`name` as color, ve.`name` as version
            FROM `enterprises_vehicles` AS ev
            INNER JOIN `status` AS s ON (s.`id`=ev.`id_status`)
            INNER JOIN `vehicles` AS v ON (v.`id`=ev.`id_vehicle`)
            INNER JOIN `models` AS m ON (m.`id`=v.`id_model`)
            INNER JOIN `colors` AS c ON (c.`id`=v.`id_color`)
            LEFT JOIN `versions` AS ve ON (ve.`id`=v.`id_version`)
            LEFT JOIN `enterprises_transactions` AS ct ON (ct.`id_enterprise`=ev.`id_enterprise`)
            WHERE ev.`id_enterprise`=?
            GROUP BY ev.`id`';
        do {
            $rs = parent::get($query, $id_enterprise);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get full lines of enterprise vehicles found
     * 
     * @param int $id_enterprise The identity of the enterprise
     * @return int The total number of records found
     */
    public static function enterprise_vehicles_numrows($id_enterprise) {
        $query = 'SELECT COUNT(*) AS numrows FROM `enterprises_vehicles` AS etov
            INNER JOIN `enterprises` AS etp ON (etp.`id`=etov.`id_enterprise`)
            WHERE etp.`id`=? AND (etp.`id_status` IS NOT NULL AND etp.`id_status`=3)';
        $rs = parent::get($query, $id_enterprise);
        return isset($rs->data) ? $rs->data->numrows : 0;
    }

    /**
     * Pegar a quantidade de veiculos disponivel para cadastro e se nao for maio que o do plano
     * 
     * @param int $id_enterprise The identity of the enterprise
     * @return int The total number of records found
     */
    public static function enterprise_autoquantity($id_enterprise) {
        $query = 'SELECT p.`autoquantity` FROM `enterprises_vehicles` AS v
                    INNER JOIN `enterprises` AS e on e.`id`=v.`id_enterprise`
                    INNER JOIN `plans` AS p on p.`id` = e.`id_plan`
                    where e.`id`=?
                    HAVING COUNT(v.`id`) < p.`autoquantity`';
        $rs = parent::get($query, $id_enterprise);
        return isset($rs->data) ? $rs->data->autoquantity : 0;
    }

    /**
     * Get the enterprise vehicles that are looking
     * 
     * @param int $pg The current page number
     * @param int $npp The number of news per pages
     * @param int $id_enterprise The identity of the enterprise
     * @return array The data of the enterprise vehicles found or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function enterprise_vehicles_search($pg, $npp, $id_enterprise) {
        if (!is_numeric($pg) || $pg < 1)
            $pg = 1;
        else
            $pg = (int) $pg;
        $query = 'SELECT v.`id`, m.`name`, `engine`, t.`name`, c.`name`,
            `firstreg`, `modelyear`, f.`name`, ctoe.`name`, stoe.`name`,
            `price`, vtop.`filename`, b.`name`, etp.`filebrand`
            ,ve.`name` AS version
            FROM `vehicles` AS v
            LEFT JOIN `vehicles_pictures` AS vtop ON (vtop.`id_vehicle`=v.`id`)
            LEFT JOIN `models` AS m ON (m.`id`=v.`id_model`)
            LEFT JOIN `brands` AS b ON (b.`id`=m.`id_brand`)
            LEFT JOIN `types` AS t ON (t.`id`=m.`id_type`)
            LEFT JOIN `colors` AS c ON (c.`id`=v.`id_color`)
            LEFT JOIN `fuels` AS f ON (f.`id`=v.`id_fuel`)
            LEFT JOIN `versions` AS ve ON (ve.`id` = v.`id_version`)
            INNER JOIN `enterprises_vehicles` AS etov ON (etov.`id_vehicle`=v.`id`)
            INNER JOIN `enterprises` AS etp ON (etp.`id`=etov.`id_enterprise`)
            LEFT JOIN `cities` AS ctoe ON (ctoe.`id`=etp.`id_city`)
            LEFT JOIN `states` AS stoe ON (stoe.`id`=ctoe.`id_state`)
            WHERE etp.`id`=? AND (etp.`id_status` IS NOT NULL AND etp.`id_status`=3)
            GROUP BY v.`id` LIMIT ' . ($pg - 1) * $npp . ', ' . $npp;
        do {
            $rs = parent::get($query, $id_enterprise);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get full lines of enterprises found
     * 
     * @return int The total number of records found
     */
    public static function enterprises_numrows() {
        $query = 'SELECT COUNT(*) AS numrows FROM `enterprises`';
        $rs = parent::get($query);
        return isset($rs->data) ? $rs->data->numrows : 0;
    }

    /**
     * Get the enterprises that are looking
     * 
     * @param int $pg The current page number
     * @param int $npp The number of news per pages
     * @return array The data of the enterprises found or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function enterprises_search($pg, $npp) {
        if (!is_numeric($pg) || $pg < 1)
            $pg = 1;
        else
            $pg = (int) $pg;
        $query = 'SELECT e.`id`, `fancyname`, e.`filepicture`, e.`filebrand`, e.`address`, e.`number`, 
                c.`name`, s.`symbol`, p.`areacode`, p.`number`, et.`paydate`, pl.`daysamount`
                FROM `enterprises` AS e 
                INNER JOIN `enterprises_transactions` AS et ON (e.id=et.id_enterprise)
                INNER JOIN `plans` AS pl ON (pl.id=e.id_plan)
                LEFT JOIN `cities` AS c ON (c.`id`=e.`id_city`) 
                LEFT JOIN `states` AS s ON (s.`id`=c.`id_state`) 
                LEFT JOIN `enterprises_phones` AS etop ON (etop.`id_enterprise`=e.`id`) 
                LEFT JOIN `phones` AS p ON (p.`id`=etop.`id_phone`) 
                WHERE DATE_ADD(et.`paydate`, INTERVAL pl.`daysamount`DAY)>CURDATE() and e.`id_status`=3
                GROUP BY e.`id` ORDER BY `fancyname` LIMIT ' . ($pg - 1) * $npp . ', ' . $npp;
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the exchanges
     * 
     * @return array The data of the exchanges
     */
    public static function exchanges() {
        $query = 'SELECT `id`,`name` FROM `exchanges` ORDER BY `name`';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the fuels
     * 
     * @return array The data of the fuels
     */
    public static function fuels() {
        $query = 'SELECT `id`,`name` FROM `fuels` ORDER BY `name`';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get full lines of guide automotive found
     * 
     * @return int The total number of records found
     */
    public static function guideautomotive_numrows() {
        $query = 'SELECT
            (SELECT COUNT(DISTINCT `id_client`) FROM `products_owners` WHERE `type`="F")+
            (SELECT COUNT(DISTINCT `id_client`) FROM `products_owners` WHERE `type`="J")
            AS numrows';
        $rs = parent::get($query);
        return isset($rs->data) ? $rs->data->numrows : 0;
    }

    /**
     * Get full lines of guide automotive products found
     * 
     * @param int $id_client The identity of the client
     * @param string $type The type of the client
     * @return int The total number of records found
     */
    public static function guideautomotive_products_numrows($id_client, $type) {
        $query = 'SELECT COUNT(*) AS numrows FROM `products_owners` AS ptoo
            INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
            WHERE ptoo.`id_client`=? AND ptoo.`type`=? AND (pcs.`id_status` IS NOT NULL AND pcs.`id_status`=3)';
        $rs = parent::get($query, $id_client, $type);
        return isset($rs->data) ? $rs->data->numrows : 0;
    }

    /**
     * Get the guide automotive products that are looking
     * 
     * @param int $pg The current page number
     * @param int $npp The number of news per pages
     * @param int $id_client The identity of the client
     * @param int $type The type of the client
     * @return array The data of the guide automotive products found or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function guideautomotive_products_search($pg, $npp, $id_client, $type) {
        if (!is_numeric($pg) || $pg < 1)
            $pg = 1;
        else
            $pg = (int) $pg;
        $query = 'SELECT p.`id`, `name`, `price`, `filepicture`
            FROM `products` AS p
            INNER JOIN `products_pictures` AS ptop ON (ptop.`id_product`=p.`id`)
            INNER JOIN `products_owners` AS ptoo ON (ptoo.`id_product`=p.`id`)
            INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
            WHERE pcs.`id_status` IS NOT NULL AND pcs.`id_status`=3 AND ptoo.`id_client`=? AND ptoo.`type`=?
            GROUP BY p.`id` LIMIT ' . ($pg - 1) * $npp . ', ' . $npp;
        do {
            $rs = parent::get($query, $id_client, $type);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the guide automotive that are looking
     * 
     * @param int $pg The current page number
     * @param int $npp The number of news per pages
     * @return array The data of the guide automotive found or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function guideautomotive_search($pg, $npp) {
        if (!is_numeric($pg) || $pg < 1)
            $pg = 1;
        else
            $pg = (int) $pg;
        $query = '(SELECT
                    "F" AS type,
                    ptoo.`id_client`,
                    cl.`name` AS clname,
                    NULL AS filepicture,
                    NULL AS filebrand,
                    cl.`address`,
                    cl.`number`,
                    c.`name`,
                    s.`symbol`,
                    p.`areacode`,
                    p.`number`,
                    ptot.`paydate`,
                    pl.`daysamount`
                FROM `products_owners` AS ptoo
                INNER JOIN `customers` AS cl ON (cl.`id`=ptoo.`id_client` AND ptoo.`type`="F")
                INNER JOIN `products_transactions` AS ptot ON (ptot.`id_client`=ptoo.`id_client` AND ptot.`type`=ptoo.`type`)
                INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
                INNER JOIN `plans` AS pl ON (pl.`id`=cl.`id_product_plan`)
                INNER JOIN `cities` AS c ON (c.`id`=cl.`id_city`)
                INNER JOIN `states` AS s ON (s.`id`=c.`id_state`)
                LEFT JOIN `customers_phones` AS ctop ON (ctop.`id_customer`=ptoo.`id_client`)
                LEFT JOIN `phones` AS p ON (p.`id`=ctop.`id_phone`)
                WHERE DATE_ADD(ptot.`paydate`, INTERVAL pl.`daysamount` DAY)>CURDATE() and pcs.`id_status`=3
                GROUP BY cl.`id`
                )
                UNION
                (SELECT
                    "J" AS type,
                    ptoo.`id_client`,
                    cl.`fancyname` AS clname,
                    `filepicture`,
                    `filebrand`,
                    cl.`address`,
                    cl.`number`,
                    c.`name`,
                    s.`symbol`,
                    p.`areacode`,
                    p.`number`,
                    ptot.`paydate`,
                    pl.`daysamount`
                FROM `products_owners` AS ptoo
                INNER JOIN `enterprises` AS cl ON (cl.`id`=ptoo.`id_client` AND ptoo.`type`="J")
                INNER JOIN `products_transactions` AS ptot ON (ptot.`id_client`=ptoo.`id_client` AND ptot.`type`=ptoo.`type`)
                INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
                INNER JOIN `plans` AS pl ON (pl.`id`=cl.`id_product_plan`)
                INNER JOIN `cities` AS c ON (c.`id`=cl.`id_city`)
                INNER JOIN `states` AS s ON (s.`id`=c.`id_state`)
                LEFT JOIN `enterprises_phones` AS etop ON (etop.`id_enterprise`=ptoo.`id_client`)
                LEFT JOIN `phones` AS p ON (p.`id`=etop.`id_phone`)
                WHERE DATE_ADD(ptot.`paydate`, INTERVAL pl.`daysamount` DAY)>CURDATE() and pcs.`id_status`=3
                GROUP BY cl.`id`
                )
                ORDER BY clname LIMIT ' . ($pg - 1) * $npp . ', ' . $npp;
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get data from miscellaneous table
     * 
     * @param string $field The field name of the data table
     * @return mixed The field value table miscellaneous
     */
    public static function miscellaneous($field) {
        $query = 'SELECT `' . $field . '` FROM `miscellaneous` LIMIT 1';
        $rs = parent::get($query);
        return isset($rs->data) ? $rs->data->$field : false;
    }

    /**
     * Get the models of vehicles
     * 
     * @param int $id_type The identity of the type of vehicle
     * @param int $id_brand The identity of the brand of vehicle
     * @param int|string $arg [optional]<br>The identity or name
     * @return array The data of the models of vehicles
     */
    public static function models($id_type, $id_brand, $arg = null) {
        $query = 'SELECT `id`, `name` FROM `models`
            WHERE `id_type`=? AND `id_brand`=?';
        if ($arg != null) {
            $query .= ' AND ';
            if (is_numeric($arg))
                $query .= '`id`=?';
            else
                $query .= '`name`=?';
        }
        $query .= ' ORDER BY `name`';
        do {
            if ($arg == null)
                $rs = parent::get($query, $id_type, $id_brand);
            else
                $rs = parent::get($query, $id_type, $id_brand, $arg);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        if ($arg != null && is_array($data) && count($data) == 1)
            $data = $data[0];
        return $data;
    }

    /**
     * Get the types of vehicles of the plan
     * 
     * @param int $id_plan The identity of the plan
     * @return array The data of the types of vehicles of the plan
     */
    public static function plan_types($id_plan) {
        $query = 'SELECT `id_type` FROM `plans_types`
            WHERE `id_plan`=?';
        do {
            $rs = parent::get($query, $id_plan);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the phones
     * 
     * @param int $id The identity of the customer or enterprise
     * @param string $type The type<br><b>F</b> if customer or <b>J</b> if enterprise
     * @return array The data of the phones
     */
    public static function phones($id, $type, $typephone = NULL) {
        $query = 'SELECT * FROM `phones` AS p ';
        if ($type == 'F')
            $query .= 'INNER JOIN `customers_phones` AS cp ON (cp.`id_phone`=p.`id`)
                WHERE cp.`id_customer`=?';
        elseif ($type == 'J')
            $query .= 'INNER JOIN `enterprises_phones` AS ep ON (ep.`id_phone`=p.`id`)
                WHERE ep.`id_enterprise`=?';
        else
            die('The type supplied is invalid.');
        $query .= ' ORDER BY p.`id_type`';
        do {
            $rs = parent::get($query, $id);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the phones types
     * 
     * @return array The data of the phones types
     */
    public static function phones_types() {
        $query = 'SELECT `id`, `name` FROM `phones_types`';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the plan
     * 
     * @param int $identity The identity
     * @return stdClass The data of the plan
     */
    public static function plan($identity) {
        $query = 'SELECT * FROM `plans` WHERE `id`=?';
        $rs = parent::get($query, $identity);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the plans
     * 
     * @param string $entitytype The entity type<br>
     *                           <b>F</b> if customer or <b>J</b> if enterprise
     * @return array The data of the plans
     */
    public static function plans($entitytype, $category) {
        if (isset($category)) {
            $category_sql = ' and category = ?';
        }
        $query = 'SELECT * FROM `plans` WHERE `entitytype`=?
        ' . $category_sql . ' ORDER BY `name`';
        do {
            if (isset($category))
                $rs = parent::get($query, $entitytype, $category);
            else
                $rs = parent::get($query, $entitytype);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the states of the private customers
     * 
     * @return array The data from the states of private customers
     */
    public static function private_states() {
        $query = 'SELECT s.`id`, s.`name` FROM `states` AS s
            INNER JOIN `cities` AS c ON (c.`id_state`=s.`id`)
            INNER JOIN `customers` AS ctm ON (ctm.`id_city`=c.`id`)
            GROUP BY 1 ORDER BY 2';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the cities of the private customers
     * 
     * @param int $id_state The identity of the state
     * @return array The data from the cities of private customers
     */
    public static function private_cities($id_state) {
        $query = 'SELECT c.`id`, c.`name` FROM `cities` AS c
            INNER JOIN `customers` AS ctm ON (ctm.`id_city`=c.`id`)
            WHERE `id_state`=? GROUP BY 1 ORDER BY 2';
        do {
            $rs = parent::get($query, $id_state);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the products latest
     * 
     * @param int $limit The number of limit
     * @return array The data of the products or<br><b>FALSE</b> if no result
     */
    public static function products_latest($limit, $id_client = NULL, $type = NULL, $id_product_current = NULL) {
        if ($id_client != NULL and $type != NULL and $id_product_current != NULL) {
            $condition = " and ptoo.`id_client`=? and ptoo.`type`=? and p.`id`<>?";
        }
        $query = 'SELECT p.`id`, p.`name`, `price`, `filepicture`
            FROM `products` AS p
            INNER JOIN `products_pictures` AS ptop ON (ptop.`id_product`=p.`id`)
            INNER JOIN `products_owners` AS ptoo ON (ptoo.`id_product`=p.`id`)
            INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
            WHERE pcs.`id_status` IS NOT NULL AND pcs.`id_status`=3 ' . $condition . '
            GROUP BY p.`id` ORDER BY `datemodified` DESC LIMIT ' . $limit;
        do {
            if ($id_client != NULL and $type != NULL and $id_product_current != NULL) {
                $rs = parent::get($query, $id_client, $type, $id_product_current);
            } else {
                $rs = parent::get($query);
            }
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get full lines of products found
     * 
     * @param array $params The search parameters
     * @return int The total number of records found
     */
    public static function products_numrows($params) {
        $query = 'SELECT COUNT(*) AS numrows FROM `products` AS p
            INNER JOIN `products_owners` AS ptoo ON (ptoo.`id_product`=p.`id`)
            INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
            LEFT JOIN `products_categories` AS pct ON (pct.`id`=p.`id_category`)
            WHERE pcs.`id_status` IS NOT NULL AND pcs.`id_status`=3';
        if (count($params) > 0) {
            $vars = array();
            foreach ($params as $key => $value) {
                $where .= ' AND ';
                switch ($key) {
                    case 'keywords':
                        $where .= 'p.`name` LIKE ?';
                        ${$key} = '%' . $value . '%';
                        break;
                    case 'category':
                        $where .= 'pct.`url` = ?';
                        ${$key} = $value;
                        break;
                    default:
                        ${$key} = null;
                        break;
                }
                if (${$key} != null)
                    $vars[] = '$' . $key;
            }
            $query .= $where;
        }
        if (empty($vars))
            $rs = parent::get($query);
        else
            eval('$rs = parent::get($query, ' . implode(',', $vars) . ');');
        return isset($rs->data) ? $rs->data->numrows : 0;
    }

    /**
     * Get the products that are looking
     * 
     * @param int $pg The current page number
     * @param int $npp The number of news per pages
     * @param array $params The search parameters
     * @return array The data of the products found or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function products_search($pg, $npp, $params) {

        if (!is_numeric($pg) || $pg < 1)
            $pg = 1;
        else
            $pg = (int) $pg;
        $query = 'SELECT p.`id`, p.`name`, `price`, `filepicture`
            FROM `products` AS p
            INNER JOIN `products_pictures` AS ptop ON (ptop.`id_product`=p.`id`)
            INNER JOIN `products_owners` AS ptoo ON (ptoo.`id_product`=p.`id`)
            INNER JOIN `products_client_status` AS pcs ON (pcs.`id_client`=ptoo.`id_client` AND pcs.`type`=ptoo.`type`)
            LEFT JOIN `products_categories` AS pct ON (pct.`id`=p.`id_category`)
            WHERE pcs.`id_status` IS NOT NULL AND pcs.`id_status`=3';
        if (count($params) > 0) {
            $vars = array();
            foreach ($params as $key => $value) {
                $where .= ' AND ';
                switch ($key) {
                    case 'keywords':
                        $where .= 'p.`name` LIKE ?';
                        ${$key} = '%' . $value . '%';
                        break;
                    case 'category':
                        $where .= 'pct.`url` = ?';
                        ${$key} = $value;
                        break;
                    default:
                        ${$key} = null;
                        break;
                }
                if (${$key} != null)
                    $vars[] = '$' . $key;
            }
            $query .= $where;
        }
        $query .= ' GROUP BY p.`id` LIMIT ' . ($pg - 1) * $npp . ', ' . $npp;
        do {
            if (empty($vars))
                $rs = parent::get($query);
            else
                eval('$rs = parent::get($query, ' . implode(',', $vars) . ');');
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the proposal
     * 
     * @param int $id The identity
     * @return array The data of the proposal
     */
    public static function proposal($id) {
        $query = 'SELECT * FROM `proposals` WHERE `id`=?';
        $rs = parent::get($query, $id);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get full lines of news proposal
     * 
     * @return int The total number of records found
     */
    public static function proposals_newsrow($id, $type) {
        $query = 'SELECT COUNT(*) AS numrows
            FROM `proposals` AS p
            INNER JOIN `vehicles` AS v ON (v.`id`=p.`id_vehicle`)';
        if ($type == 'F')
            $query .= 'INNER JOIN `customers_vehicles` AS cv ON (cv.`id_vehicle`=v.`id`)
                WHERE cv.`id_customer`=?';
        elseif ($type == 'J')
            $query .= 'INNER JOIN `enterprises_vehicles` AS ev ON (ev.`id_vehicle`=v.`id`)
                WHERE ev.`id_enterprise`=?';
        else
            die('The type supplied is invalid.');
        $query .= ' AND p.`read`=0';
        $rs = parent::get($query, $id);
        return isset($rs->data) ? $rs->data->numrows : 0;
    }

    /**
     * Get the proposals
     * 
     * @param int $id The identity of the customer or enterprise
     * @param string $type The type<br><b>F</b> if customer or <b>J</b> if enterprise
     * @return array The data of the proposals
     */
    public static function proposals($id, $type) {
        $query = 'SELECT p.`id`, p.`client`, p.`email`, p.`phone`, p.`message`,
            p.`read`, p.`date`, m.`name`, c.`name`, v.`firstreg`
            FROM `proposals` AS p
            INNER JOIN `vehicles` AS v ON (v.`id`=p.`id_vehicle`)
            LEFT JOIN `models` AS m ON (m.`id`=v.`id_model`)
            LEFT JOIN `colors` AS c ON (c.`id`=v.`id_color`)';
        if ($type == 'F')
            $query .= 'INNER JOIN `customers_vehicles` AS cv ON (cv.`id_vehicle`=v.`id`)
                WHERE cv.`id_customer`=?';
        elseif ($type == 'J')
            $query .= 'INNER JOIN `enterprises_vehicles` AS ev ON (ev.`id_vehicle`=v.`id`)
                WHERE ev.`id_enterprise`=?';
        else
            die('The type supplied is invalid.');
        $query .= ' ORDER BY p.`date` DESC';
        do {
            $rs = parent::get($query, $id);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the proposals of products
     * 
     * @param int $id The identity of the customer or enterprise
     * @param string $type The type<br><b>F</b> if customer or <b>J</b> if enterprise
     * @return array The data of the proposals
     */
    public static function proposals_products_numrows($id, $type) {
        $query = "SELECT COUNT(*) AS numrows 
                FROM `products_proposals` AS p
                INNER JOIN `products` AS pp ON (pp.`id`=p.`id_product`) 
                INNER JOIN `products_owners` AS ptoo ON (ptoo.`id_product`=pp.`id`) 
                WHERE ptoo.`type`='" . $type . "' and p.`read`=0 and ptoo.`id_client`=?";
        $query .= ' ORDER BY p.`date` DESC';
        $rs = parent::get($query, $id);
        return isset($rs->data) ? $rs->data->numrows : 0;
    }

    /**
     * Get the proposals of products
     * 
     * @param int $id The identity of the customer or enterprise
     * @param string $type The type<br><b>F</b> if customer or <b>J</b> if enterprise
     * @return array The data of the proposals
     */
    public static function proposals_products($id, $type) {
        $query = "SELECT p.`id`, p.`client`, p.`email`, p.`phone`, p.`message`, p.`read`, p.`date`,pp.`name` 
                FROM `products_proposals` AS p
                INNER JOIN `products` AS pp ON (pp.`id`=p.`id_product`) 
                INNER JOIN `products_owners` AS ptoo ON (ptoo.`id_product`=pp.`id`) 
                WHERE ptoo.`type`='" . $type . "' and ptoo.`id_client`=?";
        $query .= ' ORDER BY p.`date` DESC';
        do {
            $rs = parent::get($query, $id);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the cities of the enterprises of resales
     * 
     * @return array The cities of the enterprises or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function resales_cities() {
        $query = 'SELECT c.`id`, c.`name` FROM `cities` AS c
            INNER JOIN `enterprises` AS e ON (e.`id_city`=c.`id`)
            GROUP BY c.`id` ORDER BY c.`name`';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the enterprises of resales
     * 
     * @param int $id_city The identity of the city
     * @return array The data of the enterprises
     */
    public static function resales_enterprises($id_city) {
        $query = 'SELECT `id`, `fancyname` FROM `enterprises`
            WHERE `id_city`=? and `id_status`=3  ORDER BY `fancyname`';
        do {
            $rs = parent::get($query, $id_city);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the shields
     * 
     * @return array The data of the shields
     */
    public static function shields() {
        $query = 'SELECT `id`,`name` FROM `shields` ORDER BY `name`';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the states
     * 
     * @return array The data of the states
     */
    public static function states() {
        $query = 'SELECT `id`, `symbol`, `name` FROM `states` ORDER BY `symbol`';
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the types of vehicles
     * 
     * @param int|string $arg [optional]<br>The identity or name
     * @return array|stdClass The data of the types of vehicles
     */
    public static function types($arg = null) {
        $query = 'SELECT `id`,`name` FROM `types`';
        if ($arg != null) {
            $query .= ' WHERE ';
            if (is_numeric($arg))
                $query .= '`id`=?';
            else
                $query .= '`name`=?';
        }
        $query .= ' ORDER BY `name`';
        do {
            if ($arg == null)
                $rs = parent::get($query);
            else
                $rs = parent::get($query, $arg);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        if ($arg != null && is_array($data) && count($data) == 1)
            $data = $data[0];
        return $data;
    }

    /**
     * Get the types of vehicles
     * 
     * @param int|string $arg [optional]<br>The identity or name
     * @return array|stdClass The data of the types of vehicles
     */
    public static function versions($arg = null) {
        $query = 'SELECT `id`,`name` FROM `versions`';
        if ($arg != null) {
            $query .= ' WHERE ';
            if (is_numeric($arg))
                $query .= '`id`=?';
            else
                $query .= '`name`=?';
        }
        $query .= ' ORDER BY `name`';
        do {
            if ($arg == null)
                $rs = parent::get($query);
            else
                $rs = parent::get($query, $arg);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        if ($arg != null && is_array($data) && count($data) == 1)
            $data = $data[0];
        return $data;
    }

    /**
     * Get the models version of vehicles
     * 
     * @param int|string $arg [optional]<br>The identity or name
     * @return array|stdClass The data of the types of vehicles
     */
    public static function modelsversion($arg = NULL, $arg2 = NULL) {
        $query = 'SELECT v.`id` AS `id`,v.`name` AS `name` FROM `models_versions` AS mv
            INNER JOIN `models` AS m ON (m.`id` = mv.`id_model`)
            INNER JOIN `versions` AS v ON (v.`id` = mv.`id_version`)';
        if ($arg != null) {
            $query .= ' WHERE ';
            if (is_numeric($arg))
                $query .= 'mv.`id_model`=?';
            if (is_numeric($arg2)) {
                $query .= ' and mv.`id_version`=?';
            }
            else
                $query .= 'v.`name`=?';
        }
        $query .= ' ORDER BY v.`name`';
        do {
            if ($arg == null)
                $rs = parent::get($query);
            else
            if ($arg != null and $arg2 != null) {
                $rs = parent::get($query, $arg, $arg2);
            } else {
                $rs = parent::get($query, $arg);
            }
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        if ($arg != null && count($data) == 1)
            $data = $data[0];
        return $data;
    }

    /**
     * Get the vehicle
     * 
     * @param int $id The identity
     * @return stdClass The data of the vehicles or<br><b>FALSE</b> if no result
     */
    public static function vehicle($id) {
        $query = 'SELECT v.`id`, m.`name`, `engine`, t.`name`, b.`name`,
            `id_model`, c.`name`, `firstreg`, `modelyear`, f.`name`, `price`,
            `discount`, `new`, `km`,`doors`, `plate`, `amountviews`,`observations`,ve.`name` as version
            FROM `vehicles` AS v
            LEFT JOIN `models` AS m ON (m.`id`=v.`id_model`)
            LEFT JOIN `brands` AS b ON (b.`id`=m.`id_brand`)
            LEFT JOIN `types` AS t ON (t.`id`=m.`id_type`)
            LEFT JOIN `colors` AS c ON (c.`id`=v.`id_color`)
            LEFT JOIN `fuels` AS f ON (f.`id`=v.`id_fuel`)
            LEFT JOIN `versions` AS ve on (ve.`id` = v.`id_version`)
            WHERE v.`id`=?';
        $rs = parent::get($query, $id);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get field data and values count of vehicles
     * 
     * @param string $field The field name of the data table
     * @param array $cond [optional]<br>The conditions
     * @return boolean|array The data of the pictures of the vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function vehicle_countval($field, $cond = null) {
        $query = 'SELECT ' . $field . ', count(' . $field . ') AS count
            FROM `vehicles` AS v
            LEFT JOIN `models` AS m ON (m.`id`=v.`id_model`)
            LEFT JOIN `brands` AS b ON (b.`id`=m.`id_brand`)
            LEFT JOIN `types` AS t ON (t.`id`=m.`id_type`)
            LEFT JOIN `versions` AS ve ON (ve.`id`=v.`id_version`)
            LEFT JOIN `customers_vehicles` AS ctov ON (ctov.`id_vehicle`=v.`id`)
            LEFT JOIN `customers` AS ctm ON (ctm.`id`=ctov.`id_customer`)
            LEFT JOIN `cities` AS ctoc ON (ctoc.`id`=ctm.`id_city`)
            LEFT JOIN `enterprises_vehicles` AS etov ON (etov.`id_vehicle`=v.`id`)
            LEFT JOIN `enterprises` AS etp ON (etp.`id`=etov.`id_enterprise`)
            LEFT JOIN `cities` AS ctoe ON (ctoe.`id`=etp.`id_city`)';
        if (count($cond) > 0) {
            $where = ' WHERE ';
            $vars = array();
            foreach ($cond as $key => $value) {
                if ($where != ' WHERE ')
                    $where .= ' AND ';
                ${$key} = $value;
                switch ($key) {
                    case 'type':
                        $where .= 't.`id`=?';
                        break;
                    case 'brand':
                        $where .= 'b.`id`=?';
                        break;
                    case 'model':
                        $where .= 'm.`id`=?';
                        break;
                    case 'version':
                        $where .= 've.`id`=?';
                        break;
                    case 'initialyear':
                        $where .= 'v.`firstreg` >= ?';
                        break;
                    case 'finalyear':
                        $where .= 'v.`firstreg` <= ?';
                        break;
                    case 'initialprice':
                        $where .= 'v.`price` >= ?';
                        break;
                    case 'finalprice':
                        $where .= 'v.`price` <= ?';
                        break;
                    case 'advertiser':
                        if ($value == 'customer')
                            $where .= 'ctov.`id_vehicle` IS NOT NULL';
                        elseif ($value == 'enterprise')
                            $where .= 'etov.`id_vehicle` IS NOT NULL';
                        ${$key} = null;
                        break;
                    case 'city':
                        $where .= '(ctoc.`id`=? OR ctoe.`id`=?)';
                        ${$key . '_1'} = $value;
                        $vars[] = '$' . $key . '_1';
                        ${$key . '_2'} = $value;
                        $vars[] = '$' . $key . '_2';
                        ${$key} = null;
                        break;
                    default:
                        ${$key} = null;
                        break;
                }
                if (${$key} != null)
                    $vars[] = '$' . $key;
            }
            $query .= $where;
        }
        $query .= ' GROUP BY ' . $field . ' HAVING count('.$field.') > 0 ORDER BY count DESC, ' . $field;
        do {
            if (empty($vars))
                $rs = parent::get($query);
            else
                eval('$rs = parent::get($query, ' . implode(',', $vars) . ');');
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the details of the vehicle 
     * 
     * @param int $id_vehicle The identity of the vehicle
     * @return boolean|array The data of the details of the vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function vehicle_details($id_vehicle) {
        $query = 'SELECT * FROM `vehicles_details` AS vd
            INNER JOIN `details` AS d ON (d.`id`=vd.`id_detail`)
            WHERE `id_vehicle`=?';
        do {
            $rs = parent::get($query, $id_vehicle);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the differentials of the vehicle 
     * 
     * @param int $id_vehicle The identity of the vehicle
     * @return boolean|array The data of the differentials of the vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function vehicle_differentials($id_vehicle) {
        $query = 'SELECT * FROM `vehicles_differentials` AS vd
            INNER JOIN `differentials` AS d ON (d.`id`=vd.`id_differential`)
            WHERE `id_vehicle`=?';
        do {
            $rs = parent::get($query, $id_vehicle);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the average values of the vehicle
     * 
     * @param int $id_model The identity of the model
     * @param int $firstreg [optional]<br>The first registration
     * @param int $modelyear [optional]<br>The model year
     * @return boolean|stdClass The data of the details of the vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function vehicle_prices_average($id_model, $firstreg = null, $modelyear = null) {
        $vars = array();
        $query = 'SELECT
            AVG(`price`-`discount`) AS average,
            MIN(`price`-`discount`) AS less,
            MAX(`price`-`discount`) AS more
            FROM `vehicles` WHERE `id_model`=?';
        $vars[] = '$id_model';
        if ($firstreg != null) {
            $query .= ' AND `firstreg`=?';
            $vars[] = '$firstreg';
        }
        if ($modelyear != null) {
            $query .= ' AND `modelyear`=?';
            $vars[] = '$modelyear';
        }
        eval('$rs = parent::get($query, ' . implode(',', $vars) . ');');
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get full lines of vehicles found
     * 
     * @param array $params The search parameters
     * @return int The total number of records found
     */
    public static function vehicles_numrows($params) {
        $query = 'SELECT COUNT(*) AS numrows FROM `vehicles` AS v
            LEFT JOIN `models` AS m ON (m.`id`=v.`id_model`)
            LEFT JOIN `brands` AS b ON (b.`id`=m.`id_brand`)
            LEFT JOIN `types` AS t ON (t.`id`=m.`id_type`)
            LEFT JOIN `colors` AS c ON (c.`id`=v.`id_color`)
            LEFT JOIN `fuels` AS f ON (f.`id`=v.`id_fuel`)
            LEFT JOIN `versions` AS ve ON (ve.`id`=v.`id_version`)
            LEFT JOIN `customers_vehicles` AS ctov ON (ctov.`id_vehicle`=v.`id`)
            LEFT JOIN `customers` AS ctm ON (ctm.`id`=ctov.`id_customer`)
            LEFT JOIN `cities` AS ctoc ON (ctoc.`id`=ctm.`id_city`)
            LEFT JOIN `enterprises_vehicles` AS etov ON (etov.`id_vehicle`=v.`id`)
            LEFT JOIN `enterprises` AS etp ON (etp.`id`=etov.`id_enterprise`)
            LEFT JOIN `cities` AS ctoe ON (ctoe.`id`=etp.`id_city`)
            WHERE ((ctov.`id_status` IS NOT NULL AND ctov.`id_status`=3)
            OR (etp.`id_status` IS NOT NULL AND etp.`id_status`=3))';
        if (count($params) > 0) {
            $vars = array();
            foreach ($params as $key => $value) {
                $where .= ' AND ';
                ${$key} = $value;
                switch ($key) {
                    case 'type':
                        $where .= 't.`id`=?';
                        break;
                    case 'brand':
                        $where .= 'b.`id`=?';
                        break;
                    case 'model':
                        $where .= 'm.`id`=?';
                        break;
                    case 'version':
                        $where .= 've.`id`=? ';
                        break;
                    case 'initialyear':
                        $where .= 'v.`firstreg` >= ?';
                        break;
                    case 'finalyear':
                        $where .= 'v.`firstreg` <= ?';
                        break;
                    case 'initialprice':
                        $where .= 'v.`price` >= ?';
                        break;
                    case 'finalprice':
                        $where .= 'v.`price` <= ?';
                        break;
                    case 'advertiser':
                        if ($value == 'customer')
                            $where .= 'ctov.`id_vehicle` IS NOT NULL';
                        elseif ($value == 'enterprise')
                            $where .= 'etov.`id_vehicle` IS NOT NULL';
                        ${$key} = null;
                        break;
                    case 'city':
                        $where .= '(ctoc.`id`=? OR ctoe.`id`=?)';
                        ${$key . '_1'} = $value;
                        $vars[] = '$' . $key . '_1';
                        ${$key . '_2'} = $value;
                        $vars[] = '$' . $key . '_2';
                        ${$key} = null;
                        break;
                    default:
                        ${$key} = null;
                        break;
                }
                if (${$key} != null)
                    $vars[] = '$' . $key;
            }
            $query .= $where;
        }

        if (empty($vars))
            $rs = parent::get($query);
        else
            eval('$rs = parent::get($query, ' . implode(',', $vars) . ');');
        return isset($rs->data) ? $rs->data->numrows : 0;
    }

    /**
     * Get the pictures of the vehicle
     * 
     * @param int $id_vehicle The identity of the vehicle
     * @return boolean|array The data of the pictures of the vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function vehicle_pictures($id_vehicle) {
        $query = 'SELECT * FROM `vehicles_pictures` WHERE `id_vehicle`=?';
        do {
            $rs = parent::get($query, $id_vehicle);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the vehicles that are looking
     * 
     * @param int $pg The current page number
     * @param int $npp The number of news per pages
     * @param array $params The search parameters
     * @return array The data of the vehicles found or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function vehicles_search($pg, $npp, $params) {
        if (!is_numeric($pg) || $pg < 1)
            $pg = 1;
        else
            $pg = (int) $pg;
        $query = 'SELECT v.`id`, m.`name`, `engine`, t.`name`, c.`name`,
            `firstreg`, `modelyear`, f.`name`, ctoc.`name`, ctoe.`name`,
            stoc.`name`, stoe.`name`, `price`, vtop.`filename`, b.`name`,
            etp.`filebrand`, etp.`id`, etp.`fancyname`, `discount`, ve.`name` as version
            FROM `vehicles` AS v
            LEFT JOIN `vehicles_pictures` AS vtop ON (vtop.`id_vehicle`=v.`id`)
            LEFT JOIN `models` AS m ON (m.`id`=v.`id_model`)
            LEFT JOIN `brands` AS b ON (b.`id`=m.`id_brand`)
            LEFT JOIN `types` AS t ON (t.`id`=m.`id_type`)
            LEFT JOIN `colors` AS c ON (c.`id`=v.`id_color`)
            LEFT JOIN `fuels` AS f ON (f.`id`=v.`id_fuel`)
            LEFT JOIN `versions` AS ve ON (ve.`id` = v.`id_version`)
            LEFT JOIN `customers_vehicles` AS ctov ON (ctov.`id_vehicle`=v.`id`)
            LEFT JOIN `customers` AS ctm ON (ctm.`id`=ctov.`id_customer`)
            LEFT JOIN `enterprises_vehicles` AS etov ON (etov.`id_vehicle`=v.`id`)
            LEFT JOIN `enterprises` AS etp ON (etp.`id`=etov.`id_enterprise`)
            LEFT JOIN `cities` AS ctoc ON (ctoc.`id`=ctm.`id_city`)
            LEFT JOIN `cities` AS ctoe ON (ctoe.`id`=etp.`id_city`)
            LEFT JOIN `states` AS stoc ON (stoc.`id`=ctoc.`id_state`)
            LEFT JOIN `states` AS stoe ON (stoe.`id`=ctoe.`id_state`)
            WHERE ((ctov.`id_status` IS NOT NULL AND ctov.`id_status`=3)
            OR (etp.`id_status` IS NOT NULL AND etp.`id_status`=3))';
        if (count($params) > 0) {
            $vars = array();
            foreach ($params as $key => $value) {
                $where .= ' AND ';
                ${$key} = $value;
                switch ($key) {
                    case 'type':
                        $where .= 't.`id`=?';
                        break;
                    case 'brand':
                        $where .= 'b.`id`=?';
                        break;
                    case 'model':
                        $where .= 'm.`id`=?';
                        break;
                    case 'version':
                        $where .= 've.`id`=?';
                        break;
                    case 'initialyear':
                        $where .= 'v.`firstreg` >= ?';
                        break;
                    case 'finalyear':
                        $where .= 'v.`firstreg` <= ?';
                        break;
                    case 'initialprice':
                        $where .= 'v.`price` >= ?';
                        break;
                    case 'finalprice':
                        $where .= 'v.`price` <= ?';
                        break;
                    case 'advertiser':
                        if ($value == 'customer')
                            $where .= 'ctov.`id_vehicle` IS NOT NULL';
                        elseif ($value == 'enterprise')
                            $where .= 'etov.`id_vehicle` IS NOT NULL';
                        ${$key} = null;
                        break;
                    case 'city':
                        $where .= '(ctoc.`id`=? OR ctoe.`id`=?)';
                        ${$key . '_1'} = $value;
                        $vars[] = '$' . $key . '_1';
                        ${$key . '_2'} = $value;
                        $vars[] = '$' . $key . '_2';
                        ${$key} = null;
                        break;
                    default:
                        ${$key} = null;
                        break;
                }
                if (${$key} != null)
                    $vars[] = '$' . $key;
            }
            $query .= $where;
        }
        $query .= ' GROUP BY v.`id` LIMIT ' . ($pg - 1) * $npp . ', ' . $npp;
        do {
            if (empty($vars))
                $rs = parent::get($query);
            else
                eval('$rs = parent::get($query, ' . implode(',', $vars) . ');');
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the vehicles latest
     * 
     * @param int $limit The number of limit
     * @param boolean $featured [optional]<br>
     *                          <b>TRUE</b> if featured or <b>FALSE</b> if not
     * @return array The data of the vehicles or<br><b>FALSE</b> if no result
     */
    public static function vehicles_latest($limit, $featured = false) {
        $query = 'SELECT v.`id`, m.`name`, b.`name`, t.`name`, c.`name`,
            `firstreg`, `modelyear`, f.`name`, `price`, `discount`, `filename`
            FROM `vehicles` AS v
            INNER JOIN `vehicles_pictures` AS vtop ON (vtop.`id_vehicle`=v.`id`)
            LEFT JOIN `models` AS m ON (m.`id`=v.`id_model`)
            LEFT JOIN `brands` AS b ON (b.`id`=m.`id_brand`)
            LEFT JOIN `types` AS t ON (t.`id`=m.`id_type`)
            LEFT JOIN `colors` AS c ON (c.`id`=v.`id_color`)
            LEFT JOIN `fuels` AS f ON (f.`id`=v.`id_fuel`)
            LEFT JOIN `customers_vehicles` AS ctov ON (ctov.`id_vehicle`=v.`id`)
            LEFT JOIN `enterprises_vehicles` AS etov ON (etov.`id_vehicle`=v.`id`)
            LEFT JOIN `enterprises` AS etp ON (etp.`id`=etov.`id_enterprise`)
            WHERE ((ctov.`id_status` IS NOT NULL AND ctov.`id_status`=3)
            OR (etp.`id_status` IS NOT NULL AND etp.`id_status`=3))';
        if ($featured)
            $query .= ' AND `featured` = 1';
        $query .= ' GROUP BY v.`id` ORDER BY `datemodified` DESC LIMIT ' . $limit;
        do {
            $rs = parent::get($query);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the ad of product of the clients
     * 
     * @param int $id The identity
     * @return boolean|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function product($id_product) {
        $query = 'SELECT * FROM `products` WHERE `id`=?';
        $rs = parent::get($query, $id_product);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the ad of product of the clients
     * 
     * @param int $id The identity
     * @return boolean|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function products_ad($id_product, $type) {
        $query = 'SELECT * FROM `products_owners` AS po 
            INNER JOIN `products` AS p ON (po.`id_product`=p.`id`)
            WHERE po.`id_product` = ? and po.`type`=?';
        $rs = parent::get($query, $id_product, $type);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the ad of product of the clients
     * 
     * @param int $id The identity
     * @return boolean|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function products_owners($id_product) {
        $query = 'SELECT * FROM `products_owners` AS po 
            INNER JOIN `products` AS p ON (po.`id_product`=p.`id`)
            WHERE po.`id_product` = ?';
        $rs = parent::get($query, $id_product);
        return isset($rs->data) ? $rs->data : false;
    }

    /**
     * Get the ad of product of the clients
     * 
     * @param int $id The identity
     * @return boolean|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function products_ads($id_client, $type) {
        $query = 'SELECT * FROM `products_owners` AS po 
            INNER JOIN `products` AS p ON (po.`id_product`=p.`id`)
            WHERE po.`id_client` = ? and po.`type`=?';
        do {
            $rs = parent::get($query, $id_client, $type);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the pictures of the products
     * 
     * @param int $id_product The identity of product
     * @return boolean|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function products_pictures($id_product) {
        $query = 'SELECT * FROM `products_pictures` as p
            WHERE `id_product`=?';
        do {
            $rs = parent::get($query, $id_product);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        return $data;
    }

    /**
     * Get the products proposal
     * 
     * @param int $id_product The identity of product
     * @return boolean|stdClass The data of the ad of vehicle or<br>
     *                          <b>FALSE</b> if no result
     */
    public static function products_proposals($id) {
        $query = 'SELECT * FROM `products_proposals` where `id`=?';
        $rs = parent::get($query, $id);
        return isset($rs->data) ? $rs->data : false;
    }

    public static function products_categories($arg = null) {
        $query = 'SELECT `id`,`name`,`url` FROM `products_categories`';
        if ($arg != null) {
            $query .= ' WHERE ';
            if (is_numeric($arg))
                $query .= '`id`=?';
            else
                $query .= '`name`=?';
        }
        $query .= ' ORDER BY `name`';
        do {
            if ($arg == null)
                $rs = parent::get($query);
            else
                $rs = parent::get($query, $arg);
            if (isset($rs->data))
                $data[] = $rs->data;
        } while ($rs->fetch);
        if ($arg != null && is_array($data) && count($data) == 1)
            $data = $data[0];
        return $data;
    }

    public static function enterprises_status($id_client) {
        $query = 'SELECT e.`id`,e.`id_status`,s.`name`,p.`name`,p.`id`,e.`alertpay`,
                DATE_ADD(et.`paydate`, INTERVAL p.`daysamount` DAY) AS expired, p.`price`
                FROM `enterprises` AS e 
                INNER JOIN `plans` AS p ON p.`id`=e.`id_plan`
                INNER JOIN `status` AS s ON s.`id`=e.`id_status`
                LEFT JOIN `enterprises_transactions` AS et ON (e.`id`=et.`id_enterprise`)
                WHERE e.`id` = (SELECT `id_enterprise` FROM `enterprises_transactions`
                WHERE `id_enterprise` = e.`id`
                ORDER BY `paydate` desc LIMIT 1) AND e.`id`=?';
        $rs = parent::get($query, $id_client);
        return isset($rs->data) ? $rs->data : false;
    }

    public static function products_status($id_client, $type) {
        if ($type == 'F') {
            $client = '`customers` AS c';
        } else if ($type == 'J') {
            $client = '`enterprises` AS c';
        }
        $query = 'select c.`id`,psc.`id_status`,s.`name`,p.`name`,p.`id`,
                DATE_ADD(pt.`paydate`, INTERVAL p.`daysamount` DAY) AS expired, p.`price`, c.`alertpay`
                from `products_client_status` AS psc
                INNER JOIN `status` AS s ON (psc.`id_status` = s.`id`)
                INNER JOIN ' . $client . ' ON (c.`id`= psc.`id_client`)
                INNER JOIN `plans` AS p ON (c.`id_product_plan`=p.`id`)
                LEFT JOIN `products_transactions` AS pt ON (pt.`id_client`=c.`id` and pt.`type` = psc.`type`)
                where psc.`id_client` = ? and psc.`type`=?';
        $rs = parent::get($query, $id_client, $type);
        return isset($rs->data) ? $rs->data : false;
    }

    public static function products_exeded($id_client, $type) {
        if ($type == 'F') {
            $client = '`customers` AS c';
        } else if ($type == 'J') {
            $client = '`enterprises` AS c';
        }
        $query = 'SELECT p.`autoquantity`, COUNT(po.`id_product`),p.`id`
                    FROM `products_owners` AS po
                    INNER JOIN `enterprises` AS c ON c.`id`=po.`id_client`
                    INNER JOIN `plans` AS p ON p.`id`=c.`id_product_plan`
                    WHERE c.`id`=? and po.`type`=?
                    HAVING COUNT(po.`id_product`) < p.`autoquantity`';
        $rs = parent::get($query, $id_client, $type);
        return isset($rs->data) ? $rs->data->autoquantity : false;
    }

}